//
//  MyInvestView.m
//  TenThousand
//
//  Created by 我的 on 16/6/16.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "MyInvestView.h"
#import "MyInvestViewCell.h"
#import "ReimbursementTableViewController.h"
#import "PayBackModel.h"
#import "PayBackingModel.h"
#import "PayEndModel.h"

@interface MyInvestView ()<UITableViewDelegate,UITableViewDataSource>
{
    BOOL _isLastPage;
    
}
@property (nonatomic, strong) NSMutableArray *borrArr;
@property (nonatomic, strong) NSMutableArray *huanKuanArr;
@property (nonatomic, strong) NSMutableArray *endArr;

@end
static NSInteger currentPage;
@implementation MyInvestView

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.myInvestTableView];
}

//存储解析的数据
-(NSMutableArray *)borrArr{
    if (!_borrArr) {
        _borrArr = [NSMutableArray array];
    }
    return _borrArr;
}

-(NSMutableArray *)huanKuanArr{
    if (!_huanKuanArr) {
        _huanKuanArr = [NSMutableArray array];
    }
    return _huanKuanArr;
}

-(NSMutableArray *)endArr{
    if (!_endArr) {
        _endArr = [NSMutableArray array];
    }
    return _endArr;
}


- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    NSString *method;
    if (self.index == 0) {
        method = @"Borrow.GetBorrowListALL";
    } else
        if (self.index == 1) {
        method = @"Borrow.GetBorrowListpayment";
    } else {
        method = @"Borrow.GetBorrowListFinance";
    }
    [self getBorrowList:method WithPage:1];
    [self refreshPageWithMethod:method];
}

- (void)getBorrowList:(NSString *)method WithPage:(NSInteger)page{
    
    NSString *loginId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginId"];
    NSString *index = [NSString stringWithFormat:@"%ld",page];
    NSDictionary *dict = @{
                           @"PageIndex":index,
                           @"LoginId":loginId
                           };
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:method withParaments:dict withSuccessBlock:^(NSDictionary *object) {
        
        
        
        if (self.index == 0) {
            NSLog(@"%@",object);
            
            if (page == 1) {
                
                
                [self.borrArr removeAllObjects];
                currentPage = 1;
                _isLastPage = NO;
                NSArray *arr = [object[@"Data"] firstObject][@"ItemList"];
                for (NSDictionary *dic in arr) {
                    PayBackModel *model = [[PayBackModel alloc]initWithDictionary:dic];
                    [self.borrArr addObject:model];
                }
            }else
            {
                NSArray *arr = [object[@"Data"] firstObject][@"ItemList"];
                if (arr && [arr count]) {
                    _isLastPage = NO;
                    for (NSDictionary *dic in arr) {
                        PayBackModel *model = [[PayBackModel alloc]initWithDictionary:dic];
                        [self.borrArr addObject:model];
                    }
                }else
                {
                    _isLastPage = YES;
                }
                
            }

           
        [self.myInvestTableView reloadData];
        }else if (self.index == 1) {
            
            if (page == 1) {
                [self.huanKuanArr removeAllObjects];
                currentPage = 1;
                _isLastPage = NO;
                NSArray *arr = [object[@"Data"] firstObject][@"List"];
                for (NSDictionary *dic in arr) {
                    PayBackingModel *model = [[PayBackingModel alloc]initWithDictionary:dic];
                    [self.huanKuanArr addObject:model];
                }
            }else
            {
                NSArray *arr = [object[@"Data"] firstObject][@"List"];
                if (arr && [arr count]) {
                    _isLastPage = NO;
                    for (NSDictionary *dic in arr) {
                        PayBackingModel *model = [[PayBackingModel alloc]initWithDictionary:dic];
                        [self.huanKuanArr addObject:model];
                    }

                }else
                {
                    _isLastPage = YES;
                }
                
            }
            [self.myInvestTableView reloadData];
        }else {
            NSLog(@"%@",object);
            if (page == 1) {
                [self.endArr removeAllObjects];
                currentPage = 1;
                _isLastPage = NO;
                NSArray *arr = [object[@"Data"] firstObject][@"ItemList"];
                for (NSDictionary *dic in arr) {
                    PayEndModel *model = [[PayEndModel alloc]initWithDictionary:dic];
                    [self.endArr addObject:model];
                }
            }else
            {
                NSArray *arr = [object[@"Data"] firstObject][@"ItemList"];
                if (arr && [arr count]) {
                    _isLastPage = NO;
                    for (NSDictionary *dic in arr) {
                        PayEndModel *model = [[PayEndModel alloc]initWithDictionary:dic];
                        [self.endArr addObject:model];
                    }
                }else
                {
                    _isLastPage = YES;
                }
                
            }

            [self.myInvestTableView reloadData];
        }        
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@", error);
        [ToastView showErrorWithStatus:@"网络请求失败"];
    } progress:^(float progress) {
        
    }];
}

- (UITableView *)myInvestTableView{
    if (!_myInvestTableView) {
        _myInvestTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64-40) style:UITableViewStyleGrouped];
        _myInvestTableView.delegate = self;
        _myInvestTableView.dataSource = self;
        _myInvestTableView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.00];
        _myInvestTableView.showsVerticalScrollIndicator = NO;
        _myInvestTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    }
        return _myInvestTableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.index == 0) {
        return self.borrArr.count;
    }else if (self.index == 1) {
        return self.huanKuanArr.count;
    }
    return self.endArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyInvestViewCell *investCell = [tableView dequeueReusableCellWithIdentifier:@"MYINVESTVIEWCELL"];
    if (investCell == nil) {
        investCell = [[[NSBundle mainBundle] loadNibNamed:@"MyInvestViewCell" owner:nil options:nil] lastObject];
    }
    investCell.selectionStyle = UITableViewCellSelectionStyleNone;
    //cell添加阴影
    investCell.layer.shadowOffset =CGSizeMake(0, 1);
    investCell.layer.shadowColor = [UIColor grayColor].CGColor;
    investCell.layer.shadowRadius = 1;
    investCell.layer.shadowOpacity = .5f;
    
    if (self.index == 0) {
        investCell.ProjeceName.text = [NSString stringWithFormat:@"%@:%@",[[self.borrArr objectAtIndex:indexPath.section] ProjeceName],[[self.borrArr objectAtIndex:indexPath.section] Code]];
        investCell.Amount.text = [[self.borrArr objectAtIndex:indexPath.section] Amount];
        investCell.Tearm.text = [[self.borrArr objectAtIndex:indexPath.section] Tearm];
        investCell.Sate.text = [[self.borrArr objectAtIndex:indexPath.section] Sateqqq];
        investCell.Rate.text = [[self.borrArr objectAtIndex:indexPath.section] Rateqqq];
        investCell.interest.text = [[self.borrArr objectAtIndex:indexPath.section] interest];
        investCell.AddDate.text = [[self.borrArr objectAtIndex:indexPath.section] AddDate];

    }else if (self.index == 1) {
        investCell.ProjeceName.text = [NSString stringWithFormat:@"%@:%@",[[self.huanKuanArr objectAtIndex:indexPath.section] Project],[[self.huanKuanArr objectAtIndex:indexPath.section] Code]];
        investCell.Amount.text = [NSString stringWithFormat:@"借款本金:%@",[[self.huanKuanArr objectAtIndex:indexPath.section] MaxPrice]];
        investCell.Tearm.text = [[self.huanKuanArr objectAtIndex:indexPath.section] TeamYear];
        investCell.Sate.text = [[self.huanKuanArr objectAtIndex:indexPath.section] PaySate];
        investCell.AddDate.text = [[self.huanKuanArr objectAtIndex:indexPath.section] AddTime];
        investCell.Rate.text = [[self.huanKuanArr objectAtIndex:indexPath.section] Ratejiekuan];
        investCell.interest.text = [NSString stringWithFormat:@"借款利息:%@",[[self.huanKuanArr objectAtIndex:indexPath.section] Totaljiekuanlixi]];
    }else {
        investCell.ProjeceName.text = [NSString stringWithFormat:@"%@:%@",[[self.endArr objectAtIndex:indexPath.section] ProjeceName],[[self.endArr objectAtIndex:indexPath.section] Code]];
        investCell.Amount.text = [[self.endArr objectAtIndex:indexPath.section] Amount];
        investCell.Tearm.text = [[self.endArr objectAtIndex:indexPath.section] Tearm];
        investCell.Sate.text = [[self.endArr objectAtIndex:indexPath.section] Satexx];
        investCell.Rate.text = [[self.endArr objectAtIndex:indexPath.section] Ratexx];
        investCell.interest.text = [[self.endArr objectAtIndex:indexPath.section] interest];
        investCell.AddDate.text = [[self.endArr objectAtIndex:indexPath.section] AddDate];
    }
    
    
    return investCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
   
    if (section == 0) {
        return 1;
    }
    return 5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.index == 1) {
        ReimbursementTableViewController *reimbursementView = [[ReimbursementTableViewController alloc] init];
        
        if (self.huanKuanArr.count) {
            reimbursementView.model = self.huanKuanArr[indexPath.section];
            
        }
        reimbursementView.index = 0;
        [self.navigationController pushViewController:reimbursementView animated:YES];
    }
    
    
    
}

//MJ刷新
-(void)refreshPageWithMethod:(NSString *)method
{
    
    //下拉刷新
    self.myInvestTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^
                                  {
                                      
                                      [self getBorrowList:method WithPage:1];
                                      
                                      [self.myInvestTableView.mj_header endRefreshing];
                                  }];
    //上拉加载
    self.myInvestTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        
        
        if (_isLastPage) {
            [self.myInvestTableView.mj_footer endRefreshing];
            return;
        }
        
        [self getBorrowList:method WithPage:++currentPage];
        
        [self.myInvestTableView.mj_footer endRefreshing];
    }];
    
    
}

@end
